<?php

namespace app\backstage\controller;

use think\Controller;
use think\Request;
use app\backstage\model\Role as RoleModel;
use app\backstage\model\Auth;


class Role extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request, RoleModel $role)
    {
        if($request->isAjax()){
            
            //分页
            $pageSize = 15;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

           $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['role_name','like',"%{$params['seach_text']}%"];
            }
            
            //角色列表
            $data = $role->where($where)->order('id','desc')->limit($offset,$pageSize)->select();
            
            if($data){
                foreach($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);

                    if($v['auth_ids']){
                        //权限列表
                        $authList = Auth::whereIn('id',$v['auth_ids'])->field('auth_name')->select();

                        if($authList){
                            $v['auth_ids']='';
                            foreach($authList as $row){
                                $v['auth_ids'].=$row['auth_name'].',';
                            }

                            $v['auth_ids'] = rtrim($v['auth_ids'],',');
                        }
                    }
                }
            }

            if(!empty($where)){
                $count = count($data);
            }else{
                $count = $role->count();
            }

            $pageData = ['count'=>$count,'page'=>$page,'pageSize'=>$pageSize];

            return ['code'=>200,'roleData'=>$data,'pageData'=>$pageData];
           
        }
        return view('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(RoleModel $role)
    {   
        $authList = $role->authList();

        return view('create',['authList'=>$authList]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->isAjax()){
            
            $data = $request->post();
            $role = new RoleModel();
            
            if(!empty($data['auth_ids'])){
                $data['auth_ids'] = join(',',$data['auth_ids']);
                $data['auth_list'] = $role->createAuthList($data['auth_ids']);
            }

            $role->data($data);
            //验证参数 
            $check = $role->valiData($data);

            if($check['code'] !=200) return $check;

            //插入数据
            $role->create_time = time();
            $res = $role->allowField(true)->save();
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'插入失败'];
            }

        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {   
        $role = new RoleModel();
        $info = $role->where('id',$id)->find();
    
        $authList = $role->authList();
        
        return view('edit',['authList'=>$authList,'info'=>$info]);
        
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isAjax()){
            
            $data = $request->post();

            $role = new RoleModel();
           if(!empty($data['auth_ids'])){
                $data['auth_ids'] = join(',',$data['auth_ids']);
                $data['auth_list'] = $role->createAuthList($data['auth_ids']);
           }

            //验证参数 
            $check = $role->valiData($data);

            if($check['code'] !=200) return $check;
            unset($data['id']);
    
            //修改数据
            $res = $role->allowField(true)->update($data,['id'=>$id]);
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'修改失败','res'=>$data];
            }

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = RoleModel::destroy($id);
        
        if($res){
            return ['code'=>200];
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
    }

     /**
     * 批量删除
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function delete_all()
    {   

        if($ids = request()->post('ids')){
            $res = RoleModel::destroy($ids);
            
            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>101,'msg'=>'删除失败'];
            }
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
      
    }
}
